﻿using K9Nano.Application.Reporting;
using K9Nano.AspNetCore.Filters;
using K9Nano.Exceptions;
using K9Nano.Reporting;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.AspNetCore.Controllers;

/// <summary>
/// 报表接口
/// </summary>
[Route("admin/api/report")]
public class ReportController(IReportAppService service) : ApiControllerBase
{
    [HttpPost("run")]
    [RequestLogging(Enabled = false)]
    public virtual Task<ReportResult> Run(ReportQueryDto dto)
    {
        if (dto.PageSize < 0 || dto.PageIndex < 0) throw FriendlyException.BadRequest("分页参数不合法");
        return service.QueryAsync(dto, HttpContext.RequestAborted);
    }

    [HttpDelete("{id}")]
    public Task Delete(int id)
        => service.DeleteAsync(id, HttpContext.RequestAborted);

    [HttpGet("{id}")]
    public Task<Report?> Get(int id)
        => service.GetAsync(id, HttpContext.RequestAborted);

    [HttpGet("form/{id}")]
    public Task<ReportForm?> GetReportForm(int id)
         => service.GetReportFormAsync(id, HttpContext.RequestAborted);

    [HttpGet("summaries")]
    public Task<IEnumerable<ReportSummary>> GetReportSummaries()
         => service.GetReportSummariesAsync(HttpContext.RequestAborted);

    [HttpPost]
    public Task Save(Report report)
         => service.SaveAsync(report, HttpContext.RequestAborted);

    [HttpPut("{id}/status/{isEnabled}")]
    public Task<bool> SetStatus(int id, bool isEnabled)
         => service.SetStatusAsync(id, isEnabled, HttpContext.RequestAborted);

    [HttpPost("export")]
    public Task<string> Export(ReportExportDto dto)
        => service.ExportAsync(dto, HttpContext.RequestAborted);
}
